﻿namespace LanqiQAwebapi.Dto
{
    public class BaseResponse
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public int Code { get; set; }
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }
        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; }

       
        public static BaseResponse SetSuccess(string msg = null) => new BaseResponse()
        {
            Code = 0,
            Message = msg,
            Success = true
        };

        public static BaseResponse SetError(string errorMsg, int code = 9999) => new BaseResponse()
        {
            Code = code,
            Message = errorMsg,
            Success = false
        };

        public void Throw()
        {
            if (Success == false)
                throw new Exception(Message);
        }

    }

    /// <summary>
    /// 响应数据结构体
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseResponse<T> : BaseResponse
    {

        /// <summary>
        /// 数据
        /// </summary>
        public T Data { get; set; }

        public static BaseResponse<T> SetSuccess(T data) => new BaseResponse<T>()
        {
            Code = 0,
            Message = null,
            Success = true,
            Data = data
        };

        public static BaseResponse<T> SetResult(BaseResponse response, T data) => new BaseResponse<T>()
        {
            Code = response.Code,
            Message = response.Message,
            Success = response.Success,
            Data = data
        };



        public static BaseResponse<T> SetError(string errorMsg, int code = -1, T data = default) => new BaseResponse<T>()
        {
            Code = code,
            Message = errorMsg,
            Success = false,
            Data = data
        };

        public T ThrowGet()
        {
            if (Success == false)
                throw new Exception(Message);

            return Data;
        }
    }
}
